package dynamicprogramming;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/2 12:03
 * https://leetcode-cn.com/problems/climbing-stairs/
 */
public class ClimbStairs {

    /**
     * dp[i]：表示爬到层数n有多少种方法
     * 递推公式：dp[i] = d[i-2] + dp[i+1];
     * 初始化：dp[1] = 1 dp[2] = 2;
     * @param n
     * @return
     */
    public int climbStairs(int n) {
        if (n <= 2) {
            return n;
        }
        int[] dp = new int[3];
        dp[1] = 1;
        dp[2] = 2;
        int sum;
        for (int i = 3; i <= n; i++) {
            sum = dp[1] + dp[2];
            dp[1] = dp[2];
            dp[2] = sum;
        }
        return dp[2];
    }

    static int[] remember;

    /**
     * 递归解法
     * @param n
     * @return
     */
    public static int climbStairs1(int n) {
        remember = new int[n + 1];
        return fun(n);
    }

    public static int fun(int n) {
        if (n == 1 || n == 2) {
            return n;
        }
        if (remember[n] != 0) {
            return remember[n];
        }
        remember[n] = fun(n - 1) + fun(n - 2);
        return remember[n];
    }

    public static void main(String[] args) {
        int i = climbStairs1(4);
        System.out.println(i);
    }
}
